const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//获取当前时间
const  getNowFormatDate = () => {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1<10? "0"+(date.getMonth() + 1):date.getMonth() + 1;
	var strDate = date.getDate()<10? "0" + date.getDate():date.getDate();
	var currentdate = date.getFullYear() + seperator1  + month  + seperator1  + strDate
			+ " "  + date.getHours()  + seperator2  + date.getMinutes()
			+ seperator2 + date.getSeconds();
	return currentdate;
}

//获取当前时间戳
const getNowTimestamp = () => {
  let timestamp = new Date().getTime();
  return timestamp;
}

// 获取openId
const getOpenId = (url) => {
  let dd = wx.getStorageSync('codeMes');
  console.log(dd, 'dddd');
  wx.showLoading({
    title: '加载中',
  })  
  wx.request({
    url: `${url}`,
    method: "GET",
    data: {
      code: wx.getStorageSync('codeMes')
    },
    header: {
      "content-type": "application/json",
    },
    success: function(res) {
      wx.hideLoading();
      if (res.data.code == '200') {
        wx.setStorageSync("openid", res.data.model.openid);
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none",
          duration: 2000,
        });
      }
    },
    fail: function(err) {
      wx.hideLoading();
      console.log(err);
      wx.showToast({
        title: err.errMsg,
        icon: "none",
        duration: 2000,
      });
    },
  })
}

module.exports = {
  formatTime: formatTime,
  getNowFormatDate: getNowFormatDate,
  getNowTimestamp: getNowTimestamp,
  getOpenId: getOpenId
}
